package com.team.engine.util;

import java.lang.reflect.Field;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.team.engine.basedao.BaseDaoOperate;
import com.team.engine.basedao.BaseEntity;

/**
 * 快速dao操作类，无需继承，只要在service里注入quickDao即可，
 * 此类将自动获得表名，Id字段名，实现基本的增删改查操作  <br/>
 * 范例：
 * <pre>
 * private QuickDao<\? extends BaseEntity, ?\> quickDao;
 * 
 * set方法。。。
 * 
 * public 业务方法（）{
 * 	QuickDao<\TFilegroup, String\> dao = (QuickDao<\TFilegroup, String\>) this.quickDao;
 * 	dao.getInstance(TFilegroup.class).selectAll(int);
 * }
 * </pre>
 * @author MengLH
 * @data 2010-8-24
 * @param <T> 表实体类
 * @param <ID> 表Id字段类型
 */
@Repository
@Scope("singleton")
public class QuickDao<T extends BaseEntity, ID> extends BaseDaoOperate<T, ID>{

	/**
	 * 得到表实体的类信息
	 * @param clazz 表实体类
	 * @return
	 */
	public QuickDao<T, ID> getInstance(Class<T> clazz) {
		super.clazz = clazz;
		super.className = clazz.getSimpleName();
		
		Field[] fields = clazz.getDeclaredFields();
		for(Field f: fields){
			if(f.getName().toUpperCase().lastIndexOf("ID") > -1){
				super.idName = f.getName();
				break;
			}
		}
		return this;
	}
	
//	public static void main(String[] args) {
//		HashMap<String, String[]> map = new HashMap<String, String[]>();
//		map.put("name", new String[]{"a1"});
//		map.put("email", new String[]{"a1@ff.com"});
//		map.put("phone", new String[]{"123"});
//		map.put("password", new String[]{"a1"});
//		map.put("registerTime", new String[]{"2010-09-06"});
//		
//		TUser mapping = new AttributeMapping<TUser>(TUser.class).mapping(map);
//		System.out.println(mapping.getName());
//	}
}
